package com.carworld.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import com.carworld.config.BaseController;
import com.carworld.model.ShopOrder;
import com.carworld.model.ShopRecord;
import com.carworld.model.SysUser;
import com.carworld.utils.yilianyun.Methods;

public class PrintController extends BaseController {

	/*
	 * 打印外卖订单接口
	 */
	public void getCode() throws UnsupportedEncodingException {
		String userId = "56a469dca43e4e96a256ff73b7ae55ca";
		SysUser user = SysUser.dao.findByIdInCache(userId);
		ShopOrder shopOrder = ShopOrder.dao.getPendingOrderId(userId);
		if (shopOrder == null) {
			System.out.println("没有可打印的订单");
			return;
		}
		String orderId = String.valueOf(shopOrder.getShopOrderId());
		List<ShopRecord> orderRecord = ShopRecord.dao.getOrderDetail(orderId);
		StringBuffer orderParam = new StringBuffer();
		for (ShopRecord list : orderRecord) {
			orderParam.append("<FH><FW>" + list.getStr("spareName") + ",￥" + list.getShopRecordActualprice() + ","+ list.getShopRecordNumber()+ "份</FW></FH>\r");
		}
		StringBuffer params = new StringBuffer();
		params.append("<MN>2</MN>");
		params.append("<FH2><FW2> ***茉德精品火锅***</FW2></FH2>\r");
		params.append("................................\r");
		params.append("<FH2><FW2> --在线支付--</FW2></FH2>\r");
		params.append("下单时间："+shopOrder.getShopOrderCreatetime()+"\r");
		params.append("订单编号："+shopOrder.getShopOrderId()+"\r");
		params.append("***************商品*************\r");
		params.append(orderParam);
		params.append("................................\r");
		params.append("<FH><FW>配送费：￥1.5</FW></FH>\r");
		params.append("<FH><FW>小计：￥" + shopOrder.getShopOrderTotalprice()+ "</FW></FH>\r");
		params.append("<FH><FW>折扣：￥0</FW></FH>\r");
		params.append("*******************************\r");
		params.append("总价：￥" + shopOrder.getShopOrderTotalprice() + "\r");
		params.append("<FH><FW>"+user.getUserPosition()+"</FW></FH>\r");
		params.append("<FH><FW>" + user.getName() + "  " + user.getMobile() + "</FW></FH>\r");
		params.append("<FH><FW>备注："+shopOrder.getShopOrderRemarks()+"</FW></FH>\r");
		params.append("<FH2><FW2> **茉德精品火锅**</FW2></FH2>");
		System.out.println(params.toString());
		
		Methods.getInstance().init("1076083105", "e4cde6f55117679d4172a57de762ce05");
		String content = URLDecoder.decode(params.toString(), "utf-8");
		Methods.getInstance().addPrinter("4004608704", "237j6hy2utjm");
		String printResult = Methods.getInstance().print("4004608704", content, "0310");
		System.out.println("printResult--》" + printResult);
		if (printResult.indexOf("success") > -1) {
			System.out.println("打印成功");
			shopOrder.setShopOrderIsprint(2);
			shopOrder.update();
		}
		
	}
	
	

	/**
	 * 临时供pc端打印小票 ,根据orderId打印单子
	 * 
	 * @throws UnsupportedEncodingException 
	 */
	public void pcPrintPeceipt() throws UnsupportedEncodingException{
		String orderId = getPara("orderId");
		ShopOrder shopOrder = ShopOrder.dao.findOrderInfo(orderId);
		if (shopOrder == null) {
			System.out.println("没有可打印的订单");
			return;
		}
		List<ShopRecord> orderRecord = ShopRecord.dao.getOrderDetail(orderId);
		StringBuffer orderParam = new StringBuffer();
		for (ShopRecord list : orderRecord) {
			orderParam.append("<FH><FW>" + list.getStr("spareName") + ",￥" + list.getShopRecordActualprice() + ","+ list.getShopRecordNumber()+ "份</FW></FH>\r");
		}
		SysUser user = SysUser.dao.findByIdInCache(shopOrder.getShopOrderUser());
		StringBuffer params = new StringBuffer();
		params.append("<MN>2</MN>");
		params.append("<FH2><FW2> ***茉德精品火锅***</FW2></FH2>\r");
		params.append("................................\r");
		params.append("<FH2><FW2> --在线支付--</FW2></FH2>\r");
		params.append("下单时间："+shopOrder.getShopOrderCreatetime()+"\r");
		params.append("订单编号："+shopOrder.getShopOrderId()+"\r");
		params.append("***************商品*************\r");
		params.append(orderParam);
		params.append("................................\r");
		params.append("<FH><FW>配送费：￥1.5</FW></FH>\r");
		params.append("<FH><FW>包装费：￥1</FW></FH>\r");
		params.append("<FH><FW>小计：￥" + shopOrder.getShopOrderTotalprice()+ "</FW></FH>\r");
		params.append("<FH><FW>折扣：￥0</FW></FH>\r");
		params.append("*******************************\r");
		params.append("总价：￥" + shopOrder.getShopOrderTotalprice() + "\r");
		params.append("<FH><FW>" + user.getName() + "  " + user.getMobile() + "</FW></FH>\r");
		params.append("<FH><FW>备注："+shopOrder.getShopOrderRemarks()+"</FW></FH>\r");
		params.append("<FH2><FW2> **茉德精品火锅**</FW2></FH2>");
		System.out.println(params.toString());
		
		Methods.getInstance().init("1076083105", "e4cde6f55117679d4172a57de762ce05");
		String content = URLDecoder.decode(params.toString(), "utf-8");
		Methods.getInstance().addPrinter("4004608704", "237j6hy2utjm");
		String printResult = Methods.getInstance().print("4004608704", content, "0310");
		System.out.println("printResult--》" + printResult);
		if (printResult.indexOf("success") > -1) {
			System.out.println("打印成功");
			shopOrder.setShopOrderIsprint(2);
			shopOrder.update();
		}
	
	}
	
}
